﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using OptusMobile.Web.ServerComponents.BAL.CustomerManagement;
using OptusMobile.Web.ServerComponents.BAL.RecommenderManagement;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace OptusMobile.Web.ManagerPortal
{
    public partial class ReContract : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string cssPath = "../css/ReContract.css";

            HtmlLink link = new HtmlLink();
            link.Href = cssPath;
            link.Attributes["rel"] = "stylesheet";
            link.Attributes["type"] = "text/css";
            PlaceHolder1.Controls.Add(link);

            ShowContract();
        }
        private CustomerManager contract;
        private RecommenderManager recommender;
        private void ShowContract()
        {
            HtmlGenericControl hgc = new HtmlGenericControl();
            contract = new CustomerManager();
            var result = contract.GetCustomerContract();
            int count = result.Count;

            for(int i=0; i<count;i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<tr>");
                sb.Append("<td id=\"checkbox\">");
                CheckBox check = new CheckBox();
                check.EnableViewState = true;
                check.ID = "CheckBox" + i.ToString();
                

                LiteralControl lc =new LiteralControl(sb.ToString());
                lc.EnableViewState=true;
                hgc.Controls.Add(lc);
                hgc.Controls.Add(check);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");

                sb.Append("<td>");
                Label lblCustomerName=new Label();
                lblCustomerName.Text = result[i].customerName;
                lblCustomerName.EnableViewState = true;
                LiteralControl lc1 = new LiteralControl(sb.ToString());
                lc1.EnableViewState = true;
               
                hgc.Controls.Add(lc1);
                hgc.Controls.Add(lblCustomerName);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");

                sb.Append("<td>");
                Label lblMobileNumber = new Label();
                lblMobileNumber.Text = result[i].cellphoneNumber;
                lblMobileNumber.EnableViewState = true;
                LiteralControl lc2 = new LiteralControl(sb.ToString());
                lc2.EnableViewState = true;
                
                hgc.Controls.Add(lc2);
                hgc.Controls.Add(lblMobileNumber);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");

                sb.Append("<td>");
                Label lblHandset = new Label();
                lblHandset.Text = result[i].brandName.ToString() + result[i].handsetModole.ToString();
                lblHandset.EnableViewState = true;
                LiteralControl lc3 = new LiteralControl(sb.ToString());
                lc3.EnableViewState = true;
                
                hgc.Controls.Add(lc3);
                hgc.Controls.Add(lblHandset);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");

                sb.Append("<td>");
                Label lblPlan = new Label();
                lblPlan.Text =result[i].monthlyFee.ToString().Substring(0,2);             
                lblPlan.EnableViewState = true;
                LiteralControl lc4 = new LiteralControl(sb.ToString());
                lc4.EnableViewState = true;
                
                hgc.Controls.Add(lc4);
                hgc.Controls.Add(lblPlan);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");

                sb.Append("<td>");
                Label lblStart = new Label();
                lblStart.Text = result[i].startDate.ToString().Substring(0, result[i].startDate.ToString().IndexOf("0:"));
                lblStart.EnableViewState = true;
                LiteralControl lc5 = new LiteralControl(sb.ToString());
                lc5.EnableViewState = true;
                
                hgc.Controls.Add(lc5);
                hgc.Controls.Add(lblStart);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");

                sb.Append("<td>");
                Label lblEnd = new Label();
                lblEnd.Text = result[i].endDate.ToString().Substring(0,result[i].endDate.ToString().IndexOf("0:"));
                lblEnd.EnableViewState = true;
                LiteralControl lc6 = new LiteralControl(sb.ToString());
                lc6.EnableViewState = true;
                
                hgc.Controls.Add(lc6);
                hgc.Controls.Add(lblEnd);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");

                sb.Append("<td>");
                Label lblDuration = new Label();
                lblDuration.Text = result[i].duration.ToString();
                lblDuration.EnableViewState = true;
                LiteralControl lc7 = new LiteralControl(sb.ToString());
                lc7.EnableViewState = true;
                
                hgc.Controls.Add(lc7);
                hgc.Controls.Add(lblDuration);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");

                sb.Append("<td>");
                Label lblEmail = new Label();
                lblEmail.Text = result[i].customerEmail;
                lblEmail.EnableViewState = true;
                LiteralControl lc8 = new LiteralControl(sb.ToString());
                lc8.EnableViewState = true;
                
                hgc.Controls.Add(lc8);
                hgc.Controls.Add(lblEmail);
                sb.Remove(0, sb.Length);
                sb.Append("</td>");
                sb.Append("</tr>");
            }
            PlaceHolder2.Controls.Add(hgc);
        }
        private void SendEmail(string EachEmail, string plan, string handset, string extradata, string extramessage)
        {
            string body = "<p>Dear customer:</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; Plan:"+plan.Substring(0,2)+" &nbsp;&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; Handset:"
                +handset+" &nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; ExtraData:"+extradata+
                "</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ExtraMessage:"+extramessage+" </p><p>&nbsp;</p><p>kind regards </p><p>&nbsp;</p>";
           
            MailMessage message = new MailMessage("freewyyhl0613@hotmail.com", EachEmail, "Optus", body);
            message.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.live.com");
            client.UseDefaultCredentials = false;
            //client.Credentials = CredentialCache.DefaultNetworkCredentials;
            client.Credentials = new NetworkCredential("freewyyhl0613@hotmail.com", "wyy210360");
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            try
            {
                client.Send(message);
                this.Label2.Text="send email successful";
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                this.Label2.Text = "send email wrong";
                throw ex;
            }
        }
        private void SelectAll()
        {
            contract = new CustomerManager();
            var result = contract.GetCustomerContract();
            int count = result.Count;
            for (int i = 0; i < count; i++)
            {
                CheckBox checkbox = (CheckBox)PlaceHolder2.FindControl("CheckBox" + i.ToString());
                checkbox.Checked = true;
            }
        }
        protected void btnSent_Click(object sender, EventArgs e)
        {
            
                CheckBox cb=new CheckBox();
            
                
                contract = new CustomerManager();
                var result = contract.GetCustomerContract();
                int count = result.Count;
                for(int i=0; i<count;i++)
                {
                      //cb.Checked = ((CheckBox)sender).Checked;
                //    string ID = "CheckBox" + i.ToString();
                    CheckBox checkbox = (CheckBox)PlaceHolder2.FindControl("CheckBox"+i.ToString ());
                    if (checkbox.Checked==true)
                    {
                        string email = result[i].customerEmail.ToString();
                        string mobile = result[i].cellphoneNumber;
                        recommender = new RecommenderManager();
                        var package = recommender.RecommendPackage(mobile);
                        string plan = package.Cap.Plans.monthlyFee.ToString();
                        string brandname = package.Cap.HandsetBasicInformation.HandsetBrand.brandName;
                        string model = package.Cap.HandsetBasicInformation.handsetModole;
                        string handset = brandname + model;
                        string data = package.ExtraData.exDataPackage;
                        string message = package.ExtraMessage.exMessageAmount.ToString();
                        //string body = plan + data + message;
                        this.SendEmail(email, plan, handset,data,message);
                    }
                                                           
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            SelectAll();
        }              
    
    }
}
